import axios from "axios";
import qs from "qs";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
import pinia from "@/store/pinia";
import { useStore } from "@/store/lowCode.js";
const lowCodeStore = useStore(pinia);

// 列表页：获取表格数据
const fetchListTableData = async (modelId, currentPage, pageSize) => {
  console.log("请求基本数据");

  let fetchListTableDataRes = await axios
    .post("/lowcode/module/custom/c", qs.stringify({ id: modelId, rows: pageSize, page: currentPage }),
      {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
      })
    .then((fetchListTableDataRes) => [null, fetchListTableDataRes])
    .catch((err) => [err, null]);

  console.log("预览：");
  console.log(fetchListTableDataRes);

  if (!fetchListTableDataRes[1]) {
    console.log(fetchListTableDataRes[1].data.message);
    ElMessage({
      message: fetchListTableDataRes[1].data.message,
      type: "error",
    });
    return false;
  }

  fetchListTableDataRes = fetchListTableDataRes[1];
  console.log(fetchListTableDataRes.data.data);
  lowCodeStore.listAllData.tableData = resData;
  lowCodeStore.listAllData.total = resp.data.pageInfo.totalRecord;
};

export { fetchListTableData };
